import { useTypedSelector } from "@store/index";
import { selectToken } from "@slice/token";
import { useLocation, Navigate, Outlet } from "react-router-dom";
import React from "react";

interface Props {
    children?: React.ReactElement;
}

export default function AuthRoute(props: Props) {
    const token = useTypedSelector(selectToken);
    const location = useLocation();
    if (!token)
        return (
            <Navigate
                to="/login"
                replace={true}
                state={{ from: location.pathname + location.search + location.hash }}
            />
        );

    if (typeof props.children !== "undefined") return props.children;
    return <Outlet />;
}